<template>
  <el-form
    ref="dialogForm"
    :model="dialogForm"
    label-width="80px"
    :hide-required-asterisk="dialogForm.id ? true : false"
    :show-message="dialogForm.id ? false : true"
    :rules="dialogRules"
  >
    <el-form-item label="用户名" prop="userName">
      <el-input
        id="userName"
        v-model="dialogForm.userName"
        :disabled="dialogForm.id ? true : false"
      />
    </el-form-item>
    <el-form-item label="登录名" prop="loginName">
      <el-input
        id="loginName"
        v-model="dialogForm.loginName"
        :disabled="dialogForm.id ? true : false"
      />
    </el-form-item>
    <el-form-item label="登录密码" prop="password" v-if="!dialogForm.id">
      <el-input v-model="dialogForm.password" />
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input
        id="phone"
        v-model="dialogForm.phone"
        :disabled="dialogForm.id ? true : false"
      />
    </el-form-item>
    <el-form-item label="用户类型" prop="type">
      <el-select
        id="type"
        v-model="dialogForm.type"
        placeholder="请选择用户类型"
        :disabled="dialogForm.id ? true : false"
      >
        <el-option
          v-for="item in enums.USER_TYPE"
          :key="item.code"
          :label="item.label"
          :value="item.code"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="用户状态" v-if="dialogForm.id">
      <el-select
        v-model="dialogForm.isEnable"
        :disabled="dialogForm.id ? true : false"
      >
        <el-option
          v-for="item in enums.IS_ENABLE"
          :key="item.code"
          :label="item.label"
          :value="item.code"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="是否删除" v-if="dialogForm.id">
      <el-select
        v-model="dialogForm.isDelete"
        :disabled="dialogForm.id ? true : false"
      >
        <el-option
          v-for="item in enums.IS_DELETE"
          :key="item.code"
          :label="item.label"
          :value="item.code"
        >
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="创建人" v-if="dialogForm.id">
      <el-input
        v-model="dialogForm.createBy"
        :disabled="dialogForm.id ? true : false"
      />
    </el-form-item>
    <el-form-item label="创建时间" v-if="dialogForm.id">
      <el-input
        v-model="dialogForm.createTime"
        :disabled="dialogForm.id ? true : false"
      />
    </el-form-item>
    <el-form-item class="operate-btns" v-if="!dialogForm.id">
      <el-button size="small" @click="closeModel">取消</el-button>
      <el-button
        size="small"
        :loading="loading"
        type="primary"
        @click="onSubmit('dialogForm')"
        >确认</el-button
      >
    </el-form-item>
  </el-form>
</template>
<script>
import { handleConfirm } from "@/utils/messageBox";
import { Users } from "@/api/system";
import { mapState } from "vuex";

export default {
  props: {
    dialogForm: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      loading: false,
      dialogRules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        loginName: [
          { required: true, message: "请输入登录名", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入正确的手机号", trigger: "blur" },
        ],
        type: [
          { required: true, message: "请选择用户类型", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapState({ enums: (state) => state.enums.enums }),
  },
  watch: {
    dialogForm: {
      handler(newName, oldName) {},
      deep: true,
    },
  },
  created() {},
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.isAdd();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    isAdd() {
      let msg = this.dialogForm.id ? "编辑" : "新增";
      handleConfirm(`确定要${msg}人员信息吗？`, "warning", "提示")
        .then((res) => {
          this.doAdd();
        })
        .catch((err) => {
          // do something cancel
        });
    },
    doAdd() {
      this.loading = true;
      Users("/insert", {
        ...this.dialogForm,
      })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success(res.message);
            this.$refs["dialogForm"].resetFields();
            this.$emit("cancelDialog", true);
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    closeModel() {
      this.$emit("cancelDialog", false);
    },
  },
};
</script>
<style scoped lang="scss">
.operate-btns {
  text-align: right;
  margin: 20px 0 0;
}
.el-select {
  width: 100%;
}
::v-deep .el-input.is-disabled .el-input__inner {
  color: #666666;
  background-color: #ffffff;
}
</style>
